﻿using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using AesonBaseFramework.TestData;
using AesonXamlControls.Interaction.AesonBusyIndicator;
using AesonXamlControls.Test.Views.CustomPopupWithReturn;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.PubSubEvents;

namespace AesonXamlControls.Test.Views.HomePage
{
    public class HomePageViewModel : BindableBase
    {
        #region Field
        private string _message;
        public string Message
        {
            get { return _message; }
            set { SetProperty(ref this._message, value); }
        }


        private bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }
            set { SetProperty(ref this.isBusy, value); }
        }
        #endregion


        public HomePageViewModel(IEventAggregator ea)
        {
            eag = ea;
            this.Message = "";
            this.NotificationCommand = new DelegateCommand(NotifyMethode);
            this.NotificationRequest = new InteractionRequest<INotification>();

            this.ConfirmationCommand = new DelegateCommand(ConfirmMethode);
            this.ConfirmationRequest = new InteractionRequest<IConfirmation>();

            this.CustomPopupCommand = new DelegateCommand(CustomPopupMethode);
            this.CustomPopupViewRequest = new InteractionRequest<INotification>();

            this.CustomPopupWithReturnCommand = new DelegateCommand(CustomPopupVithReturnMethode);
            this.CustomPopupViewWithReturnRequest = new InteractionRequest<GetDataModel>();

            this.AesonPopupWithReturnCommand = new DelegateCommand(AesonPopupVithReturnMethode);
            this.AesonPopupViewWithReturnRequest = new InteractionRequest<GetDataModel>();

            this.ShowBusyCommand = new DelegateCommand(ShowBusy);
        }

        #region Notification

        public ICommand NotificationCommand { get; set; }
        public InteractionRequest<INotification> NotificationRequest { get; private set; }

        private void NotifyMethode()
        {
            // By invoking the Raise method we are raising the Raised event and triggering any InteractionRequestTrigger that
            // is subscribed to it.
            // As parameters we are passing a Notification, which is a default implementation of INotification provided by Prism
            // and a callback that is executed when the interaction finishes.
            this.NotificationRequest.Raise(
                new Notification { Content = "My Message", Title = "My Title" },
                n => { this.Message = "The user was notified."; });
        }

        #endregion

        #region Confirmation

        public ICommand ConfirmationCommand { get; set; }
        public InteractionRequest<IConfirmation> ConfirmationRequest { get; private set; }

        private void ConfirmMethode()
        {
            this.ConfirmationRequest.Raise(
                new Confirmation { Content = "Confirmation Message", Title = "Confirmation" },
                c =>
                {
                    Message = c.Confirmed ? "The user accepted." : "The user cancelled.";
                });
        }


        #endregion

        #region CustomPopupContent

        public ICommand CustomPopupCommand { get; set; }
        public InteractionRequest<INotification> CustomPopupViewRequest { get; private set; }

        private void CustomPopupMethode()
        {

            var p = new Person { Name = "MyName", Mail = "MyEmail" };
            this.CustomPopupViewRequest.Raise(
            new Notification { Content = p, Title = "My Title" },
                n => { this.Message = "Custom popup view showed"; });
        }

        #endregion

        #region CustomPopupViewWithreturn

        public ICommand CustomPopupWithReturnCommand { get; set; }
        public InteractionRequest<GetDataModel> CustomPopupViewWithReturnRequest { get; private set; }

        private void CustomPopupVithReturnMethode()
        {

            Person p = DataManager.GetPersonData().ToArray()[0];
            GetDataModel d = new GetDataModel { Person = p, Title = "Choose mail for " + p.Name };

            this.CustomPopupViewWithReturnRequest.Raise(d,
                returned =>
                {
                    if (returned != null && returned.Confirmed)
                    {
                        this.Message = "City " + returned.SelectedItem.Name + " choosed for " + returned.Person.Name;
                    }
                });
        }

        #endregion

        #region AesonPopupViewWithreturn

        IEventAggregator eag { get; set; }
        public ICommand AesonPopupWithReturnCommand { get; set; }

        public InteractionRequest<GetDataModel> AesonPopupViewWithReturnRequest { get; private set; }

        private void AesonPopupVithReturnMethode()
        {

            Person p = DataManager.GetPersonData().ToArray()[0];
            GetDataModel d = new GetDataModel { Person = p, Title = "Choose mail for " + p.Name };

            this.AesonPopupViewWithReturnRequest.Raise(d,
                returned =>
                {
                    if (returned != null && returned.Confirmed && returned.SelectedItem != null)
                    {
                        this.Message = "City " + returned.SelectedItem.Name + " choosed for " + returned.Person.Name;
                    }
                });

        }


        #endregion

        #region ShowBusy
        public ICommand ShowBusyCommand { get; set; }
        private async void ShowBusy()
        {
            IsBusy = true;
            eag.GetEvent<BusyEvent>().Publish(new BusyEvent() { Message = "First Taks..." });
            await Task.Delay(2000);
            eag.GetEvent<BusyEvent>().Publish(new BusyEvent() { Message = "Second Taks..." });
            await Task.Delay(2000);
            eag.GetEvent<BusyEvent>().Publish(new BusyEvent() { Message = "Third Taks..." });
            await Task.Delay(2000);
            eag.GetEvent<BusyEvent>().Publish(new BusyEvent() { Message = "Finish" });
            await Task.Delay(500);
            IsBusy = false;

        }




        #endregion

    }


}
